package com.qingke.Qiximybatis.business;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import com.qingke.Qiximybatis.QixiApp;
import com.qingke.Qiximybatis.QixiConsole;
import com.qingke.Qiximybatis.QixiDao;
import com.qingke.Qiximybatis.command.UserCommand;
import com.qingke.Qiximybatis.mapper.InvitationMapper;
import com.qingke.Qiximybatis.mapper.MyUserMapper;
import com.qingke.Qiximybatis.sqlclass.Invitation;
import com.qingke.Qiximybatis.sqlclass.MyUser;

public class Personal9  extends UserCommand{
	
	public Personal9(MyUser user) {
		super(user);
	}
	@Override
	public void executeUserCommand() {
		SqlSession session = QixiDao.mybatisSession();
		System.out.println("欢迎进入对象邀请界面：");
		System.out.println("可以list 查看用户信息！");
		int inviteId = Integer.parseInt(QixiConsole.askUserInput("请输入你所要邀请人的ID：>"));
		String datetime = QixiConsole.askUserInput("请输入约会的日期(格式：yyyy-MM-dd HH:mm:ss):>");
		DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date myDate2 = null;
		try {
			myDate2 = dateFormat2.parse(datetime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println(myDate2);
		InvitationMapper inviM = session.getMapper(InvitationMapper.class);
		int id = QixiApp.getInstance().getUser().getId();
		Invitation inv = new Invitation();
		inv.setInviteeUserId(inviteId);
		inv.setInviterUserId(id);
		inv.setInviteTime(myDate2);
		
		inviM.insert(inv);
		session.commit();
		String relation = relationShip();
		String InviteeUser = QixiApp.getInstance().getUser().getName();
		String gender = QixiApp.getInstance().getUser().getGender();
		if(gender.equalsIgnoreCase("M")){
			gender="先生";
		}else{
			gender="女士";
		}
		MyUser InviterUser = new MyUser();
		MyUserMapper userM = session.getMapper(MyUserMapper.class);
		InviterUser = userM.getOneUser(inviteId);
		String invgender = InviterUser.getGender();
		if(invgender.equalsIgnoreCase("M")){
			invgender="先生";
		}else{
			invgender="女士";
		}
		int x=(int)(Math.random()*300);
		System.out.println("您的请求已发送！请注意查看回复！");
		System.out.println("邀请者："+InviteeUser+gender+"盛情邀请"+InviterUser.getName()+invgender+"，"+relation+"成功几率"+x+"%");
		System.out.println("");
		session.close();
		
	}
	
	public static String  relationShip(){
		String s = QixiConsole.askUserInput("和对方是什么关系(Friends, family, friends, people around, classmates, lovers, crush, And_no_longer, In_love)");
		while(true){
			if(s.equalsIgnoreCase("Friends")||s.equalsIgnoreCase("family")||s.equalsIgnoreCase("friends")||
					s.equalsIgnoreCase("people_around")||s.equalsIgnoreCase("classmates")||s.equalsIgnoreCase("In_love")){
				s="约会";
				return s;
			}else if(s.equalsIgnoreCase("lovers")||s.equalsIgnoreCase("crush")){
				s="脱单";
				return s;
			}else if(s.equalsIgnoreCase("And_no_longer")){
				s="老友相见";
				return s;
			}else {
				System.out.println("输入的内容不符合要求，请重新输入！");
			}
		}
	}
}
